package com.ipet.item.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ipet.item.pojo.SPU;
import com.ipet.item.pojo.SPUDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SPUMapper extends BaseMapper<SPU> {
    @Select("select * from tb_spu")
    @Results({
            @Result(id = true , property = "id" , column = "id"),
            @Result(property = "spuDetail" ,column = "id",javaType = SPUDetail.class ,
             one = @One(select = "com.ipet.item.mapper.SPUDetailMapper.findById"))
    })
    List<SPU> findAllSpu();
    @Select("select * from tb_spu where title like CONCAT('%' , #{title} , '%') ")
    @Results({
            @Result(id = true , property = "id" , column = "id"),
            @Result(property = "spuDetail" ,column = "id",javaType = SPUDetail.class ,
                    one = @One(select = "com.ipet.item.mapper.SPUDetailMapper.findById"))
    })
    List<SPU> findSPUByTitle(String title);
    @Select("select * from tb_spu where saleable = #{saleable}")
    @Results({
            @Result(id = true , property = "id" , column = "id"),
            @Result(property = "spuDetail" ,column = "id",javaType = SPUDetail.class ,
                    one = @One(select = "com.ipet.item.mapper.SPUDetailMapper.findById"))
    })
    List<SPU> findSPUBySaleable(Integer saleable);

//    批量上架下架
    @Update({"<script> UPDATE tb_spu SET saleable = #{saleable} "
            + "WHERE id IN "
            + "<foreach  collection = 'listId' item = 'id' index = 'index' open = '(' separator= ',' close = ')' >"
            + "	#{id} "
            + "</foreach>"
            + "</script>"})
    Integer saleableBatch(Integer saleable , List<String> listId);
//批量删除
    @Delete({"<script> delete from tb_spu  "
            + "WHERE id IN "
            + "<foreach  collection = 'list' item = 'id' index = 'index' open = '(' separator= ',' close = ')' >"
            + "	#{id} "
            + "</foreach>"
            + "</script>"
    })
    Integer deleteSpuBatch(List<String> list);
}
